// pages/manage-address/index.js
const areaData = require('../../utils/areaData')
const {getNearbySchools, userAddAddress,getAllSchools} = require('../../apiConfig/apiList/luban')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      city: '',
      schoolId: '',
      detailAddress: '',
      phone: '',
      schoolName:''
    },
    tipFlag:false,
    latitude: '',
    longitude: '',
    schoolList: [
      // { text: '地址1', value: 0 },
      // { text: '地址2', value: 1 },
    ],
    value1: 0,
    adress: '',
    showAddress : false,
    showAreaName : '',
    areaList: areaData.areaList
  },
  onSelectTip(e){
    const selectedTip = e.currentTarget.dataset.item;
    this.setData({ 'formData.schoolName': selectedTip.schoolName,tipFlag:false });
    console.log(this.data)
    this.setData({
      'showAreaName':selectedTip?selectedTip.province+'/'+selectedTip.city+'/'+selectedTip.district:'',
      'formData.schoolId': selectedTip.id
    })
  },
  onInputSchoolName(e){
    console.log('onInputSchoolName')
    const value = e.detail.value;
    if(value.length<3){
      this.setData({
       'showAreaName':'',
       'formData.schoolId': '',
       tipFlag:false
      })
      return;
    }
    getAllSchools({pageNum:'1',pageSize:'9999',schoolName:value}).then(res=>{
      if(res.code == 200){
        this.setData({ 'formData.schoolName': value,schoolList:res.rows,tipFlag:true
   })
      }
    })
    // let schoolList = [{schoolName:'adfadf',id:'12',province:'222',city:'12',district:'adf'},{schoolName:'adf阿尔阿荣',id:'3',province:'27722',city:'1772',district:'ad77f'}]
  },
  getCity: function(e) {
    this.setData({
      'formData.city': e.detail.value
    })
  },
  getAddress: function(e) {
    this.setData({
      'formData.detailAddress': e.detail.value
    })
  },
  getScholLd: function(e) {
    let selected = ""
    for (let i in this.data.schoolList) {
      console.log('1',this.data.schoolList[i].value == e.detail)
      if(this.data.schoolList[i].value == e.detail){
        selected = this.data.schoolList[i];
        break;
      }
    }
    this.setData({
      'showAreaName':selected?selected.province+'/'+selected.city+'/'+selected.district:'',
      'formData.schoolId': e.detail
    })
  },
  getPhone: function(e) {
    this.setData({
      'formData.phone': e.detail.value
    })
  },
  onClickArea(){
    this.setData({
      showAddress : true
    })
  },
  selectArea(e){
    console.log(e.detail.values)
    let data = e.detail.values;
    let temp = []
    data.forEach(item=>{
      temp.push(item.name);
    })
    this.setData({
      showAddress : false,
      curAreaList: data,
      showAreaName: temp.join('/')
    })
  },
  closeArea(e){
    this.setData({
      showAddress : false
    })
  },
  onClose(e){
    console.log(234234234)
  },
  saveData(e){
    console.log(234234, this.data)
    let params = {
      schoolId: this.data.formData.schoolId,
      phone: this.data.formData.phone,
      detailAddress: this.data.formData.detailAddress,
    }
    userAddAddress(params).then(res=>{
      if(res.code == 200){
        let temp = wx.getStorageSync('loginData');
        temp.authToken = res.data.authToken;
        temp.loginUser = res.data.loginUser;
        wx.setStorageSync('loginData', temp);
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
      console.log('saveDatares', res);

    })
    console.log('saveData');
  },
  // saveData(e){
  //   console.log(234234234)
  //   let pages = getCurrentPages();
  //   let currPage = pages[pages.length - 1];
  //   let prevPage = pages[pages.length - 2];
  //   currPage.setData({
  //     msg: newtitle
  //   })
  //   prevPage.setData({
  //     msg: newtitle
  //   })
  // },
  getLocal(){
    // const that = this
    // wx.getLocation({  
    //   type: 'wgs84', // 默认为 wgs84 的 gps 坐标，如果要返回直接给 openLocation 使用的火星坐标，可传入 'gcj02'  
    //   success: function(res) {  
    //     console.log('纬度: ' + res.latitude);  
    //     console.log('经度: ' + res.longitude);    
    //     that.setData({  
    //       latitude: res.latitude,  
    //       longitude: res.longitude  
    //     });
    //     let params = {
    //       longitude: res.longitude,
    //       latitude: res.latitude,
    //       schoolId: wx.getStorageSync('loginData').schoolId || '',
    //     }
    //     getNearbySchools(params).then(res=>{
    //       console.log('getNearbySchools: ', res.data);
    //       let data = res.data;
    //       let list = [];
    //       data.forEach(item=>{
    //         list.push({text: item.schoolName, value: item.id, province:item.province, city:item.city, district:item.district});
    //       })
    //       that.setData({
    //         schoolList: list
    //       });
    //       console.log(3453453, that.data.schoolList)
    //     })
    //   },  
    //   fail: function(err) {  
    //     console.error('获取经纬度失败: ', err);  
    //   }  
    // });  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getLocal();
    console.log('areaData', areaData)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})